package com.sskj.base;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.sskj.base.model.UserViewModel;

public abstract class MVPFragment<P extends BasePresenter> extends BaseFragment implements BaseView {

    protected P mPresenter;
    private boolean isFirstToUser = true;
    public static UserViewModel userViewModel;
    protected String TAG;
    public static boolean isLogin;
    public static String account;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getName();
        mPresenter = getPresenter();
        mPresenter.attachView(this);
        getLifecycle().addObserver(mPresenter);
        userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
        userViewModel.getUser(getActivity()).observe(this, user -> {
            if (user != null) {
                isLogin = true;
                account = user.getAccount();
            } else {
                isLogin = false;
                account = "";
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initView();
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (isFirstToUser) {
                onFirstToUser();
                isFirstToUser = false;
            }
        }
    }

    public void onFirstToUser() {

    }

    public abstract P getPresenter();

}
